package com.hgy.fxui.dao;

import com.hgy.fxui.model.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserDao {
    @Select("SELECT * FROM user WHERE username = #{username}")
    User findByUsername(String username);

    @Select("SELECT * FROM user")
    List<User> findAll();

    @Select("SELECT * FROM user WHERE username LIKE CONCAT('%', #{search}, '%') OR real_name LIKE CONCAT('%', #{search}, '%')")
    List<User> search(String search);

    @Insert("INSERT INTO user(username, password, real_name, role, create_time) VALUES(#{username}, #{password}, #{realName}, #{role}, NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(User user);

    @Update("UPDATE user SET username = #{username}, real_name = #{realName}, role = #{role}" +
            ", password = CASE WHEN #{password} IS NULL OR #{password} = '' THEN password ELSE #{password} END " +
            "WHERE id = #{id}")
    void update(User user);

    @Delete("DELETE FROM user WHERE id = #{id}")
    void delete(Long id);
}
